<?php
	function lookupCell($gameID,$sesskey,$x,$y) {
		wwdb();
		$result = mysql_query("SELECT * FROM players WHERE sesskey=$sesskey");
		if ($result != null) {
			$row = mysql_fetch_assoc($result);
			$memberID = $row["memberID"];
			$result = mysql_query("SELECT * FROM waffle WHERE gameID=$gameID AND memberID=$memberID AND x=$x AND y=$y");
			if ($result != null) {
				$row = mysql_fetch_assoc($result);
				return $row["letter"];
			}
		}
	}
	
	function drawWaffle($gameID,$playerID,$showScores) {
		wwdb();
		echo "<div class='tally' style='clear:both'>";
		$result = mysql_query("SELECT playerName FROM players WHERE memberID=$playerID");
		$row = mysql_fetch_assoc($result);
		$name = $row["playerName"];
		$result = mysql_query("SELECT * FROM scores WHERE gameID=$gameID AND memberID=$playerID");
		$row = mysql_fetch_assoc($result);
		$score = $row["thisGameScore"];
		echo "<h1><a class='name' href='leaderboard.php?username=$name'>$name</a> - $score points</h1>";
		//display waffle
		echo "<div class='waffleWrapper'><div class='waffle'>";
		$w = mysql_query("SELECT * FROM scores WHERE gameID=$gameID AND memberID=$playerID");
		$row = mysql_fetch_assoc($w);
		$letters = $row["letters"];
		$i=0;
		for ($x=1;$x<=5;$x++) {
			for ($y=1;$y<=5;$y++) {
				echo "<div class='cellWrapper' id='$x$y'><div class='letter cell'>" . $letters[$i++] . "</div></div>";
			}
			echo "<div style='clear:both'></div>";
		}
		echo "</div></div>"; //waffle
		if ($showScores) displayWords($gameID, $playerID);
		echo "</div>"; //tally
	}
	
	function findWords($gameID, $playerID, $requestID = -1) {
		wwdb();
		$w = mysql_query("SELECT letters FROM scores WHERE gameID=$gameID AND memberID=$playerID");
		if ($row = mysql_fetch_assoc($w)) {
			$letters = $row["letters"];
			$i=0;
			for ($x=0;$x<5;$x++) for ($y=0;$y<5;$y++) $waffle[$x][$y] = $letters[$i++];
			$lengths = array(5,4,3);
			$ptValues = array(10,5,1);
			for($p=0;$p<3;$p++) { //for each point value
				for ($x=0;$x<5;$x++) { //for each row/column
					for ($k=0;$k<=5-$lengths[$p];$k++) {
						//for each subset of letters $lengths long in that row/column
						$wordAcross = "";
						$wordDown = "";
						for ($y=$k;$y<$lengths[$p]+$k;$y++) { //for each letter
							$wordAcross .= $waffle[$x][$y];
							$wordDown .= $waffle[$y][$x];
						}
						mysql_query("INSERT INTO allWords VALUES ($gameID, $playerID, '$wordAcross', $ptValues[$p], $requestID)");
						mysql_query("INSERT INTO allWords VALUES ($gameID, $playerID, '$wordDown', $ptValues[$p], $requestID)");
					}
				}
			}
			
			//now delete any non-words
			mysql_query("UPDATE allWords LEFT JOIN wordlist ON allWords.word=wordlist.word SET allWords.word='x' WHERE wordlist.word IS NULL");
			mysql_query("DELETE FROM allWords WHERE word='x'");

			$results = mysql_query("SELECT SUM(value) AS total FROM allWords WHERE gameID=$gameID AND memberID=$playerID AND requestID=$requestID");
			$scoreRow = mysql_fetch_assoc($results);
			$score = $scoreRow['total'];
			//no requestID means it's called only to get the score, not the words
			if ($requestID<0) mysql_query("DELETE FROM allWords WHERE gameID=$gameID AND memberID=$playerID AND requestID<0");
			return $score;
		}
		else return null;
	}

	function displayWords($gameID,$playerID) {
		wwdb();
		$requestID = mt_rand();
		$totalScore = findWords($gameID, $playerID, $requestID);
		if ($totalScore != null) { //display wordlist
			$myScore = 0;
			foreach (array(10,5,1) as $ptValue) {
				$wl=mysql_query("SELECT * FROM allWords WHERE value=$ptValue AND gameID=$gameID AND memberID=$playerID AND requestID=$requestID");
				echo "<div class='wordlist'>";
				while ($row = mysql_fetch_assoc($wl)) {
					echo "<div class='word'>" . $row['word'] . "</div>";
					echo "<div class='value'>" . $row['value'] . "</div>";
					$myScore += $row['value'];
				}
				echo "</div>";
			}
		}
		else echo "ERROR";
		mysql_query("DELETE FROM allWords WHERE gameID=$gameID AND memberID=$playerID AND requestID=$requestID");
	}
?>